package canglang.monitor.cases.dao;

import canglang.monitor.cases.model.CaseSixModel;
import canglang.monitor.cases.to.SearchSixTO;
import canglang.monitor.core.base.dao.BaseDAO;
import canglang.monitor.core.base.pagination.PaginatedListHelper;
import canglang.monitor.core.base.util.DateUtil;
import canglang.monitor.core.base.util.StringUtil;

import java.util.*;

@SuppressWarnings("unchecked")
public class CaseSixDAO extends BaseDAO {
    public Integer addCaseSix(CaseSixModel caseSixModel) {
        String sql = "addCaseSix";
        Map map = objectToMap(caseSixModel);
        String cnt = StringUtil.convertEmpty2Null(caseSixModel.getContent());
        map.put("content", cnt);
        return (Integer) getSqlMapClientTemplate().insert(sql, map);
    }

    public CaseSixModel getCaseSix(int caseSixId) {
        String sql = "getCaseSix";
        Map map = new HashMap();
        map.put("caseSixId", caseSixId);
        return (CaseSixModel) getSqlMapClientTemplate().queryForObject(sql, map);
    }

    public CaseSixModel saveCaseSix(CaseSixModel caseSixModel) {
        String sql = "saveCaseSix";
        Map map = objectToMap(caseSixModel);
        String cnt = StringUtil.convertEmpty2Null(caseSixModel.getContent());
        map.put("content", cnt);
        getSqlMapClientTemplate().update(sql, map);
        return this.getCaseSix(caseSixModel.getCaseSixId());
    }

    public void deleteCaseSix(int caseSixId) {
        String sql = "deleteCaseSix";
        Map map = new HashMap();
        map.put("caseSixId", caseSixId);
        getSqlMapClientTemplate().delete(sql, map);
    }

    public Integer searchCount(SearchSixTO searchTO) {
        Map map = new HashMap();
        map.put("deptId", searchTO.getDeptId());
        map.put("dateFrom", searchTO.getDateFrom());
        if (searchTO.getDateTo() != null) {
            map.put("dateTo", DateUtil.add(searchTO.getDateTo(), Calendar.DATE, 1));
        }
        map.put("createId", StringUtil.convertEmpty2Null(searchTO.getCreateId()));
        return (Integer) getSqlMapClientTemplate().queryForObject("searchCaseSixCount", map);
    }

    public List searchList(PaginatedListHelper helper, SearchSixTO searchTO) {
        Map map = new HashMap();
        map.put("deptId", searchTO.getDeptId());
        map.put("dateFrom", searchTO.getDateFrom());
        if (searchTO.getDateTo() != null) {
            map.put("dateTo", DateUtil.add(searchTO.getDateTo(), Calendar.DATE, 1));
        }
        map.put("createId", StringUtil.convertEmpty2Null(searchTO.getCreateId()));        
        map = constructPaginationMap(helper, map);
        return getSqlMapClientTemplate().queryForList("searchCaseSixList", map);
    }

    public List getCaseSixList(SearchSixTO searchTO) {
        Map map = new HashMap();
        int deptId = searchTO.getDeptId();
        Date dateFrom = searchTO.getDateFrom();
        Date dateTo = searchTO.getDateTo();
        map.put("deptId", deptId);
        map.put("dateFrom", dateFrom);
        if (dateTo != null) {
            map.put("dateTo", DateUtil.add(dateTo, Calendar.DATE, 1));
        }
        return getSqlMapClientTemplate().queryForList("getCaseSixList", map);
    }
}
